<?php

//require_once ('deploy/lib/class.dbModel.php');

class db_students extends dbModel
{
	protected $sSource = "students";
	
	//get modules (by student_code, class_code or both)
	function listStudents($module_code = false, $class_code = false)
	{
		
		//if there is no student code given
		if(!$module_code && $class_code) {
			
			//sql query
			$sSql = "SELECT 
						s.student_code, s.student_name 
					FROM 
						students s, connect_student_class csc 
					WHERE 
						csc.class_code LIKE '$class_code%' 
					AND 
						s.student_code = csc.student_code";
			
			$this->trashcan();
			
			//fake de dbModel functie
			$this->run_query($sSql);
			$this->oBench->start_mark();
			$this->iTotal = mysql_num_rows($this->rResult);
			$this->oBench->add_mark('counted rows');
			return $this->iTotal > 0;
		
		//if there is no class code given
		} else if(!$class_code && $module_code) {
			
			//sql query
			$sSql = "SELECT 
						s.student_code, s.student_name
					FROM 
						students s, connect_module_class cmc, connect_student_class csc
					WHERE 
						cmc.module_code LIKE '$module_code%'
					AND 
						cmc.class_code = csc.class_code
					AND 
						s.student_code = csc.student_code";
			
			$this->trashcan();
			
			//fake de dbModel functie
			$this->run_query($sSql);
			$this->oBench->start_mark();
			$this->iTotal = mysql_num_rows($this->rResult);
			$this->oBench->add_mark('counted rows');
			return $this->iTotal > 0;
		
		//if both are given
		} else if($class_code && $module_code){
		
			//sql query
			$sSql = "SELECT 
						s.student_code, s.student_name 
					FROM 
						students s, connect_module_class cmc, connect_student_class csc
					WHERE 
						cmc.module_code LIKE '$module_code%'
					AND 
						cmc.class_code LIKE '$class_code%'
					AND 
						csc.class_code = cmc.class_code
					AND 
						s.student_code = csc.student_code";
						
			
			$this->trashcan();
			
			//fake de dbModel functie
			$this->run_query($sSql);
			$this->oBench->start_mark();
			$this->iTotal = mysql_num_rows($this->rResult);
			$this->oBench->add_mark('counted rows');
			return $this->iTotal > 0;
			
		}
		else
		{
			//$this->aCols('module_name', 'module_code');
		}
	}
	function listStudentRatings($module_code, $class_code = false)
	{
		
		if($module_code && $class_code)
		{
		$sSql = "SELECT s.student_code, s.student_name, r.mark
					FROM students s
					LEFT JOIN connect_module_class cms ON (
					cms.module_code = '$module_code'
					) AND
					(cms.class_code = '$class_code')
					LEFT JOIN connect_student_class csc ON (
					csc.class_code = cms.class_code
					)
					LEFT JOIN ratings r ON 
					(r.student_code = csc.student_code) AND (r.module_code = cms.module_code)
					WHERE csc.student_code = s.student_code
					GROUP BY s.student_code";
			}
			elseif($module_code && !$class_code)
			{
				$sSql = "SELECT s.student_code, s.student_name, r.mark
					FROM students s
					LEFT JOIN connect_module_class cms ON (
					cms.module_code = '$module_code'
					) 
					LEFT JOIN connect_student_class csc ON (
					csc.class_code = cms.class_code
					)
					LEFT JOIN ratings r ON 
					(r.student_code = csc.student_code) AND (r.module_code = cms.module_code)
					WHERE csc.student_code = s.student_code
					GROUP BY s.student_code";
			}
			$this->trashcan();
			
			//fake de dbModel functie
			$this->run_query($sSql);
			$this->oBench->start_mark();
			$this->iTotal = mysql_num_rows($this->rResult);
			$this->oBench->add_mark('counted rows');
			return $this->iTotal > 0;
	} 
	public function set_class ($sClass)
	{
		$this->trashcan();
		
		$this->sSource		= "connect_student_class";
		$this->aBelongs_to	= Array(
				"student_id"	=> "students.student_code",
				"class_id"		=> "classes.class_code"			//is eig niet nodig met zo'n simpele tabel.. just in case
		);
		$this->search($sClass, "class_id");
	}
	
	
	/**
	 * Finds students by stu nr or/and by name
	 *
	 * @param String $sInput
	 */
	public function search_student ($sInput)
	{
		$this->search($sInput, Array("student_code", "student_name"));
	}
}

?>
